<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="./js/axios.min.js"></script>
    <script src="./js/api.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.9.1/jquery.js"></script>
  </head>
  <body>
    <input type="text" name="" id="" />
    <input type="text" name="" id="" />
    <input type="button" name="" id="" value="登录" />
    <script>
      // jq  查询 dom

      // 用户点击登录的时候发起 登录请求，请求成功以后 跳转到 首页
      let isUsername = false;
      let isPassword = false;

      let ipts = document.querySelectorAll("input");

      $("input")
        .eq(0)
        .blur(function () {
          let reg = /^[a-z]\w{5,17}$/i;
          if (reg.test(this.value)) {
            isUsername = true;
          } else {
            isUsername = false;
          }
        });

      $("input")
        .eq(1)
        .blur(function () {
          let reg = /^\S{6,16}$/;
          if (reg.test(this.value)) {
            isPassword = true;
          } else {
            isPassword = false;
          }
        });

      // 点击登录的发起 登录请求
      $("input")
        .eq(2)
        .click(function () {
          if (isUsername && isPassword) {
            // 发起登录请求
            let username = $("input").eq(0).val();
            let password = $("input").eq(1).val();

            console.log(loginAPI);

            $.get(baseURL + loginAPI, { username, password }).then(function (
              data
            ) {
              console.log(data); //
              localStorage.setItem("uid", data.data.id);
              localStorage.setItem("username", data.data.username);
              localStorage.setItem("token", data.data.token);
              alert(data.msg);
              if (data.code == 1) {
                // alert("index.html");
                location.href = "index.html";
              }
            });

            /*  axios
              .get(loginAPI, {
                params: { username, password },
              })
              .then((res) => {
                let { data } = res;
                console.log(data);
                localStorage.setItem("uid", data.data.id);
                localStorage.setItem("username", data.data.username);
                localStorage.setItem("token", data.data.token);
                alert(data.msg);
                if (data.code == 1) {
                  // alert("index.html");
                  location.href = "index.html";
                }
              }); */
          }
        });
    </script>
  </body>
</html>
